﻿package mortal.game.view.daily.render
{
    import Message.Client.*;
    import Message.DB.Tables.*;
    import com.gengine.resource.*;
    import com.mui.controls.*;
    import com.mui.core.*;
    import com.mui.display.*;
    import extend.language.*;
    import flash.events.*;
    import mortal.common.global.*;
    import mortal.common.tools.*;
    import mortal.component.gconst.*;
    import mortal.game.cache.*;
    import mortal.game.manager.*;
    import mortal.game.resource.*;
    import mortal.game.view.common.*;
    import mortal.game.view.common.display.*;
    import mortal.game.view.daily.*;

    public class DailyCellRenderer extends GCellRenderer
    {
        private var _selectFilter:ScaleBitmap;
        private var _bg:GBitmap;
        private var _icon:GImageBitmap;
        private var _name:GTextFiled;
        private var _lv:BitmapNumberText;
        private var _vitality:GTextFiled;
        private var _rewardIcon:GBitmap;
        private var _starPanel:StarPanel;
        private var _plan:GTextFiled;
        private var _state:GBitmap;
        private var _operBtn:GButton;
        private var _btnFlyBoot:GLoadedButton;
        private var _isResCompl:Boolean;

        public function DailyCellRenderer()
        {
            return;
        }// end function

        private function onClickOperBtnHandler(event:MouseEvent) : void
        {
            DailyUtil.oper(this.itemData);
            return;
        }// end function

        private function onClickBtnFlyBoot(event:MouseEvent) : void
        {
            DailyUtil.oper(this.itemData, true);
            return;
        }// end function

        private function get itemData() : TDailyViewConfig
        {
            return _data as TDailyViewConfig;
        }// end function

        override public function set data(param1:Object) : void
        {
            super.data = param1;
            this.setData();
            return;
        }// end function

        private function setData() : void
        {
            if (_data && this._isResCompl)
            {
                this._icon.imgUrl = this.itemData.icon + ".jpg";
                this._name.text = this.itemData.name;
                this._name.toolTipData = Language.getStringByParam(70628, this.itemData.name, this.itemData.minLevel);
                this._lv.text = this.itemData.minLevel.toString();
                this._vitality.text = Language.getStringByParam(70405, this.itemData.personActivity);
                this._btnFlyBoot.visible = false;
                EffectManager.glowFilterUnReg(this._operBtn);
                this.setBtnLabel();
                this.setPlanTxt();
                this.setStar();
                this.filters = Cache.instance.role.entityInfo.level >= this.itemData.minLevel ? (null) : ([FilterConst.colorFilter]);
            }
            return;
        }// end function

        private function setBtnLabel() : void
        {
            if (this.itemData.operType == EDailyOperType._EDailyOperTypeModule)
            {
                this._operBtn.label = Language.getString(70404);
            }
            else if (this.itemData.operType == EDailyOperType._EDailyOperTypePathing)
            {
                this._btnFlyBoot.visible = true;
                this._operBtn.label = Language.getString(70403);
            }
            if (this.itemData.expStar > 0 && Cache.instance.task.needLevelToCompleteTask != null && Cache.instance.daily.hasTime(this.itemData))
            {
                this._operBtn.filters = [FilterConst.guideFilter];
            }
            else
            {
                this._operBtn.filters = [];
            }
            return;
        }// end function

        private function setPlanTxt() : void
        {
            var _loc_2:String = null;
            var _loc_3:String = null;
            var _loc_4:Array = null;
            var _loc_5:int = 0;
            var _loc_6:int = 0;
            var _loc_1:* = Cache.instance.daily;
            this._plan.textColor = GlobalStyle.colorLvUint;
            if (int(this.itemData.times) > 0)
            {
                if (int(_loc_1.data.times[this.itemData.type]) >= int(this.itemData.times))
                {
                    this._state.visible = true;
                    this._plan.textColor = GlobalStyle.colorHuang2Uint;
                    this._plan.text = int(this.itemData.times) + "/" + this.itemData.times;
                }
                else
                {
                    this._state.visible = false;
                    this._plan.text = int(_loc_1.data.times[this.itemData.type]) + "/" + this.itemData.times;
                }
            }
            else if (this.itemData.normalOpenDt)
            {
                this.setBtnFilters(this.itemData.normalOpenDt);
                _loc_2 = DailyUtil.getTimeStr(this.itemData.normalOpenDt);
                if (this.itemData.openWeekDay)
                {
                    _loc_3 = "";
                    _loc_4 = this.itemData.openWeekDay.split("#");
                    _loc_5 = _loc_4.length;
                    _loc_6 = 0;
                    while (_loc_6 < _loc_5)
                    {
                        
                        _loc_3 = _loc_3 + GameDefConfig.instance.getChineseNum(int(_loc_4[_loc_6]));
                        _loc_6++;
                    }
                    this._plan.text = Language.getStringByParam(70407, _loc_3 + _loc_2);
                }
                else
                {
                    this._plan.text = Language.getStringByParam(70408, _loc_2);
                }
                this._state.visible = false;
            }
            else
            {
                this._plan.text = "" + Language.getString(71033);
                this._state.visible = false;
            }
            return;
        }// end function

        private function setBtnFilters(param1:String) : void
        {
            var _loc_2:int = 0;
            var _loc_3:Array = null;
            var _loc_4:Boolean = false;
            var _loc_5:Date = null;
            var _loc_6:Date = null;
            var _loc_7:Date = null;
            var _loc_8:Date = null;
            var _loc_9:Date = null;
            if (param1.indexOf("#") != -1)
            {
                _loc_3 = param1.split("#");
                _loc_5 = DateUtil.getDateByHourAndMin(String(_loc_3[0]));
                _loc_6 = DateUtil.getDateByHourAndMin(String(_loc_3[1]));
                _loc_4 = DateUtil.isInDate(_loc_5, _loc_6);
                _loc_2 = _loc_5.day;
            }
            else if (param1.indexOf("&") != -1)
            {
                _loc_3 = param1.split("&");
                _loc_7 = DateUtil.getDateByHourAndMin(String(_loc_3[0]));
                _loc_4 = DateUtil.isInDate(new Date(_loc_7.time - 5 * 60 * 1000), new Date(_loc_7.time + 5 * 60 * 1000));
                if (_loc_4 == false)
                {
                    _loc_8 = DateUtil.getDateByHourAndMin(String(_loc_3[1]));
                    _loc_4 = DateUtil.isInDate(new Date(_loc_8.time - 5 * 60 * 1000), new Date(_loc_8.time + 5 * 60 * 1000));
                }
                _loc_2 = _loc_7.day;
            }
            else
            {
                _loc_9 = DateUtil.getDateByHourAndMin(param1);
                _loc_4 = DateUtil.isInDate(new Date(_loc_9.time - 5 * 60 * 1000), new Date(_loc_9.time + 5 * 60 * 1000));
                _loc_2 = _loc_9.day;
            }
            if (this.itemData.openWeekDay)
            {
                _loc_4 = _loc_4 && this.itemData.openWeekDay.indexOf(_loc_2.toString()) != -1;
            }
            if (Cache.instance.role.entityInfo.level >= this.itemData.minLevel && _loc_4)
            {
                EffectManager.glowFilterReg(this._operBtn, [FilterConst.colorGlowFilter(GlobalStyle.colorLvUint)]);
            }
            return;
        }// end function

        private function setStar() : void
        {
            if (this.itemData.expStar > 0)
            {
                this._rewardIcon.bitmapData = GlobalClass.getBitmapData(ImagesConst.DailyTxt1);
                this._starPanel.setStarNum(this.itemData.expStar, this.itemData.expStar);
            }
            else if (this.itemData.coinStar > 0)
            {
                this._rewardIcon.bitmapData = GlobalClass.getBitmapData(ImagesConst.DailyTxt5);
                this._starPanel.setStarNum(this.itemData.coinStar, this.itemData.coinStar);
            }
            else if (this.itemData.itemStar > 0)
            {
                this._rewardIcon.bitmapData = GlobalClass.getBitmapData(ImagesConst.DailyTxt12);
                this._starPanel.setStarNum(this.itemData.itemStar, this.itemData.itemStar);
            }
            return;
        }// end function

        private function onResCompl() : void
        {
            this._isResCompl = true;
            this._state.bitmapData = GlobalClass.getBitmapData(ImagesConst.DailyTxt9);
            pushUIToDisposeVec(UIFactory.gBitmap(ImagesConst.DailyTxt11, 160, 11, this));
            this.setData();
            return;
        }// end function

        override public function set selected(param1:Boolean) : void
        {
            super.selected = param1;
            this._selectFilter.visible = param1;
            return;
        }// end function

        override protected function createDisposedChildrenImpl() : void
        {
            super.createDisposedChildrenImpl();
            this._selectFilter = UIFactory.bg(-1, -1, 398, 92, this, ImagesConst.selectFilter);
            this._bg = UIFactory.gBitmap("", 0, 0, this);
            pushUIToDisposeVec(UIFactory.bg(12, 11, 70, 70, this, ImagesConst.ShopItemBg));
            this._icon = UIFactory.gImageBitmap("", 17, 16, this);
            this._name = UIFactory.gTextField("", 89, 10, 78, 20, this);
            this._lv = UIFactory.bitmapNumberText(180, 10, "DailyNormalNum.png", 19, 20, -9, this, 9);
            this._vitality = UIFactory.gTextField("", 89, 32, 220, 20, this, GlobalStyle.textFormatLv);
            this._rewardIcon = UIFactory.gBitmap("", 88, 54, this);
            this._starPanel = UIFactory.getUICompoment(StarPanel, 124, 55, this);
            this._starPanel.setStarStyle(ImagesConst.GuildWarStarS, ImagesConst.GuildWarStarSGray, 20);
            this._plan = UIFactory.gTextField("", 155, 10, 220, 20, this, GlobalStyle.textFormatPutong.right());
            this._state = UIFactory.gBitmap("", 288, 15, this);
            this._operBtn = UIFactory.gButton("", 312, 58, 62, 22, this);
            this._operBtn.configEventListener(MouseEvent.CLICK, this.onClickOperBtnHandler);
            this._btnFlyBoot = UIFactory.gLoadedButton(ImagesConst.MapBtnFlyBoot_upSkin, 295, 60, 16, 18, this);
            this._btnFlyBoot.configEventListener(MouseEvent.CLICK, this.onClickBtnFlyBoot);
            LoaderHelp.addResCallBack(ResFileConst.daily, this.onResCompl);
            return;
        }// end function

        override protected function disposeImpl(param1:Boolean = true) : void
        {
            super.disposeImpl(param1);
            LoaderHelp.removeResEvent(ResFileConst.daily, this.onResCompl);
            _data = null;
            this._isResCompl = false;
            this._selectFilter.dispose(param1);
            this._bg.dispose(param1);
            this._icon.dispose(param1);
            this._name.dispose(param1);
            this._lv.dispose(param1);
            this._vitality.dispose(param1);
            this._rewardIcon.dispose(param1);
            this._starPanel.dispose(param1);
            this._plan.dispose(param1);
            this._state.dispose(param1);
            this._operBtn.filters = [];
            this._operBtn.dispose(param1);
            this._btnFlyBoot.dispose(param1);
            this._selectFilter = null;
            this._bg = null;
            this._icon = null;
            this._name = null;
            this._lv = null;
            this._vitality = null;
            this._rewardIcon = null;
            this._starPanel = null;
            this._plan = null;
            this._state = null;
            this._operBtn = null;
            this._btnFlyBoot = null;
            return;
        }// end function

    }
}
